<!DOCTYPE html>
<html>
<head>
	<title>个人中心</title>
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
	<link type="text/css" rel="stylesheet" href="../css/bootstrap.css">
	<link type="text/css" rel="stylesheet" href="../css/style.css">
	<script type="text/javascript" src="../js/jquery.min.js"></script>
	<script type="text/javascript" src="../js/bootstrap.min.js"></script>
	<script type="text/javascript" src="../layer/layer.js"></script>
	<script type="text/javascript" src="../js/cart.js"></script>
	<script type="text/javascript" src="../js/sha1.js"></script>
    <script type="text/javascript" src="../js/auth.js"></script>
	<script type="text/javascript" src="../js/CookieShop.js"></script>
</head>
<body>
    <div class="header">
       <div class="container">
           <nav class="navbar navbar-default" role="navigation">
               <div class="navbar-header">
                   <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
                       <span class="sr-only">Toggle navigation</span>
                       <span class="icon-bar"></span>
                       <span class="icon-bar"></span>
                       <span class="icon-bar"></span>
                   </button>
                   <h1 class="navbar-brand"><a href="./index"></a></h1>
               </div>
               <!--navbar-header-->
               <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
                   <ul class="nav navbar-nav">
                       <li><a href="./index.html">首页</a></li>
                       <li class="dropdown">
                           <a href="#" class="dropdown-toggle" data-toggle="dropdown">商品分类<b class="caret"></b></a>
                           <ul class="dropdown-menu multi-column columns-2">
                               <li>
                                   <div class="row">
                                       <div class="col-sm-12">
                                           <h4>商品分类</h4>
                                           <ul class="multi-column-dropdown" id="goodsTypeMenu">
                                               <li><a class="list" href="./goods_list.html">全部系列</a></li>
                                           </ul>
                                       </div>
                                   </div>
                               </li>
                           </ul>
                       </li>
                       <li><a href="./goodsrecommend_list.html?recommendTypeId=2" id="hotListMenu">热销</a></li>
                       <li><a href="./goodsrecommend_list.html?recommendTypeId=3" id="newListMenu">新品</a></li>
                   </ul>
                   <!--/.navbar-collapse-->
               </div>
               <!--//navbar-header-->
           </nav>
           <div class="header-info">
               <div class="header-right search-box">
                   <a href="javascript:;"><span class="glyphicon glyphicon-search" aria-hidden="true"></span></a>
                   <div class="search">
                       <form class="navbar-form" action="./goods_search.html">
                           <input type="text" class="form-control" name="keyword">
                           <button type="submit" class="btn btn-default" aria-label="Left Align">搜索</button>
                       </form>
                   </div>
               </div>

               <div class="header-right cart">
                   <a href="./goods_cart.html">
                       <span class="glyphicon glyphicon-shopping-cart" aria-hidden="true"><span class="card_num">0</span></span>
                   </a>
               </div>
               <div class="clearfix"> </div>
           </div>
           <div class="clearfix"> </div>
       </div>
   </div>
	<!--account-->
	<div class="account">
		<div class="container">
			<div class="register">
					<div class="register-top-grid">
						<h3>个人中心</h3>
						<form action="" method="post" id="ff">
						<input type="hidden" name="id" id="id">
						<!-- 收货信息 start -->
						<h4>收货信息</h4>
						<div class="input">
							<span>收货人<label></label></span>
							<input type="text" name="firstName" id="firstName" placeholder="请输入收货">
						</div>
						<div class="input">
							<span>收货电话</span>
							<input type="text" name="phone" id="phone" placeholder="请输入收货电话">
						</div>
						<div class="input">
							<span>收货地址</span>
							<input type="text" name="address" id="address" placeholder="请输入收货地址">
						</div>
						<div class="register-but text-center">
						   <input type="button" class="btn-primary" value="提交" onclick="updateMyProfileUser()">
						</div>
						<!-- 收货信息 end -->
						</form>
						<hr>
						<form action="" method="post" id="ffchangePassword">
						<h4>安全信息</h4>
						<div class="input">
							<span>原密码</span>
							<input type="password" name="password" id="password" placeholder="请输入原密码">
						</div>
						<div class="input">
							<span>新密码</span>
							<input type="password" name="newPassword" id="newPassword" placeholder="请输入新密码">
						</div>
						<div class="input">
							<span>确认新密码</span>
							<input type="password" name="confirmNewPassword" id="confirmNewPassword" placeholder="请确认新密码">
						</div>
						<div class="clearfix"> </div>
						<div class="register-but text-center">
						   <input type="button" class="btn-primary" value="提交" onclick="changeMyPasswordUser()">
						</div>
						</form>
					</div>

				<div class="clearfix"> </div>
			</div>
	    </div>
	</div>
	<!--//account-->
	<div class="footer">
        <div class="container">
            <div class="text-center">

            </div>
        </div>
    </div>
</body>
<script type="text/javascript">
$(function(){
    if (!isLogin()){
      window.location.href="../../custpages/index.html";
    }
    decorateMainMenu("center");
    findMyProfileUser();
});

function findMyProfileUser(){
	$.ajax({
		type: "post",
		url: "../controller/profileController/findMyProfileUser",
		dataType: 'json',
		success: function(data, textStatus) {
			if (data.success) {
				var rows = data.rows;
				$("#ff").find("#id").val(rows["id"]);
				$("#ff").find("#firstName").val(rows["firstName"]);
				$("#ff").find("#address").val(rows["address"]);
				$("#ff").find("#phone").val(rows["phone"]);
			}
		},
		complete : function(XMLHttpRequest, textStatus) {
		},
		error : function(XMLHttpRequest,textStatus,errorThrown) {
			alert("Error:"+textStatus);
			alert(errorThrown.toString());
		}
	});
}

function changeMyPasswordUser(){
	var password = $("#ffchangePassword").find("#newPassword").val();
	var oldPassword = $("#ffchangePassword").find("#password").val();
	var confirmPassword = $("#ffchangePassword").find("#confirmNewPassword").val();
	if (isBlank(oldPassword)){
        layer.msg("旧密码不可为空！", {icon:2,time:3000}, function(){
              });
		return;
	}
	if (isBlank(password)||isBlank(confirmPassword)){
        layer.msg("新密码不可为空！", {icon:2,time:3000}, function(){
              });
		return;
	}
	if (oldPassword == password){
        layer.msg("新密码和旧密码不可相同！", {icon:2,time:3000}, function(){
              });
		return;
	}
	if (password!=confirmPassword){
        layer.msg("新密码不匹配！", {icon:2,time:3000}, function(){
              });
		return;
	}
	$.ajax({
		type: "post",
		url: "../controller/profileController/changeMyPasswordUser",
		data:  {
			password:hex_sha1(password),
			oldPassword:hex_sha1(oldPassword),
		},
		dataType: 'json',
		success: function(data, textStatus) {
			if (data.success) {
			   layer.msg("成功修改密码！", {icon:1,time:3000}, function(){
                  });
			} else if (!data.success) {
                layer.msg("修改密码失败！", {icon:2,time:3000}, function(){
                  });
			}
		},
		complete : function(XMLHttpRequest, textStatus) {
		},
		error : function(XMLHttpRequest,textStatus,errorThrown) {
			alert("Error:"+textStatus);
			alert(errorThrown.toString());
		}
	});
}

function updateMyProfileUser(){
	if (isBlank($("#ff").find("#firstName").val())){
        layer.msg("必须字段用户名为空！", {icon:2,time:3000}, function(){
                  });
		return;
	}
	$.ajax({
		type: "post",
		url: "../controller/profileController/frontUpdateMyProfileUser",
		data: JSON.stringify({
			firstName:$("#ff").find("#firstName").val(),
			address:$("#ff").find("#address").val(),
			phone:$("#ff").find("#phone").val(),
		}),
		dataType: 'json',
		contentType:"application/json;charset=UTF-8",
		success: function(data, textStatus) {
			if (data.success) {
                layer.msg("更新用户资料成功！", {icon:1,time:3000}, function(){
                  });
				findMyProfileUser();
			} else if (data.success == false){
                layer.msg("更新用户资料失败！", {icon:1,time:3000}, function(){
                  });
				findMyProfileUser();
			}
			},
		complete : function(XMLHttpRequest, textStatus) {
		},
		error : function(XMLHttpRequest,textStatus,errorThrown) {
			alert("Error:"+textStatus);
			alert(errorThrown.toString());
		}
	});
}
</script>
</html>
